import React, { PureComponent } from 'react';
import './index.css';
import ProjectForm from '../forms/project';
import BridgeForm from '../forms/bridge';
import SwitchBridge from '../forms/switchBridge';
import PierForm from '../forms/pier';
import UploadRoads from '../forms/uploadGroupRoad';
import SelectRefRoads from '../forms/selectRoads';
import SwitchProject from '../forms/switchProject';
import ConfigSpans from '../forms/configSpan';
import StructureForm from '../forms/structure';
import BindStructure from '../forms/bindStructure';
import ConfigStructure from '../forms/structureConfig';
import StructureSelect from '../structureSelect';
import Formular from '../forms/formular';
import Calculate from '../forms/calculate'
import CusPropsEdit from '../forms/cusPropsEdit'
import CusTable from '../forms/cusTable'
import RelateProp from '../forms/relateProp'
import PropsEdit from '../forms/propsEdit'
import CrossSection from '../forms/crossSection'
import BeamLenModal from '../forms/beamLenModal'
import PinnerModal from '../forms/pinnerModal'
import PositionModal from '../forms/positionModal'
import IconMultiSelect from "../forms/iconMultiSelect"
import Adminicula from '../forms/adminicula'
import AdminiculaDetail from '../forms/adminiculaDetail'
import ModalDrag from '../modalDrag/index';
import CrossSectionProp from '../forms/crossSectionProp';
import CrossSectionDetail from '../forms/crossSectionDetail';
import AdminicularForm from '../forms/adminicularForm';

import { Modal, Button } from 'antd';
import { connect } from 'dva';

class ToolConfigModal extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            confirmLoading: false,
            formular: "",
            footer: "default",
            context: {},
        }
        this.setModalVisible = this.setModalVisible.bind(this)
        this.getFormular = this.getFormular.bind(this)
    }
    setModalVisible(bool) {
        this.setState({
            visible: bool
        })
    }
    componentDidMount() {
        if (this.props.onLoaded) {
            this.props.onLoaded(this);
        }
    }
    getFormular(lar) {
        this.setState({
            formular: lar
        })
        this.props.setFormular(lar)
    }

    /**
     * 
     */
    parseModelContent = (code) => {
        let { project, refresh, treeContext, formContext, goInnerPage, dictlist } = this.props;
        let { extra } = this.state
        this.setState({
            code
        })
        if (code === 'addProject') {
            // 项目
            return (
                <ProjectForm
                    mode="add"
                    refresh={refresh}
                    project={project}
                    onLoaded={(context) => {
                        this.setState({ context });
                    }}
                />);
        } else if (code === 'editProject') {
            // 项目
            return (
                <ProjectForm
                    mode="edit"
                    refresh={refresh}
                    project={project}
                    onLoaded={(context) => {
                        this.setState({ context });
                    }}
                />);
        } else if (code === 'changeProject') {
            // 项目切换
            return (
                <SwitchProject
                    parent={this}
                    onLoaded={(context) => {
                        this.setState({ context });
                    }}
                />);
        } else if (code === 'addBridge') {
            // 桥梁
            return (<BridgeForm

                treeContext={this.props.treeContext}
                projectId={this.props.project.id}
                mode='add'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'editBridge') {
            // 桥梁
            return (<BridgeForm

                treeContext={this.props.treeContext}
                projectId={this.props.project.id}
                mode='edit'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'switchBridge') {
            // 桥梁切换
            return (<SwitchBridge
                mode='add'
                parent={this}
                getSocketNews={this.props.getSocketNews}
                showCommond={this.props.showCommond}
                goInnerPage={goInnerPage}
                closeBg={this.props.closeBg}
                showBg={this.props.showBg}
                dictlist={dictlist}
                treeContext={this.props.treeContext}
                project={this.props.project}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'uploadGroupRoads') {
            return (<UploadRoads
                treeContext={this.props.treeContext}
                projectId={this.props.project.id}
                mode='add'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code == "structureSelect") {
            return (<StructureSelect
                formContext={formContext}
                treeContext={treeContext}
                goInnerPage={goInnerPage}
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);

        } else if (code === 'uploadGroupStructures') {
            return (<StructureForm
                projectId={project.id}
                mode='add'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'bindStructure') {
            return (<BindStructure
                spans={spans}
                currentBridge={currentBridge}
                projectId={project.id}
                offset={offset}
                maxY={maxY}
                mode='add'
                updateModal={this.updateModal}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'configStructure') {
            return (<ConfigStructure
                propss={this.props.configingStructure}

                mode='add'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'addPier') {
            // 桥墩
            return (<PierForm
                projectId={project.id}
                mode='add'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'editPier') {
            // 桥墩
            return (<PierForm
                projectId={project.id}
                mode='edit'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'formular') {
            // 桥墩
            return (<Formular
                extra={extra}
                mode='edit'
                getFormular={this.getFormular}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'configBridgeSpan') {
            // 布跨设计
            return (<ConfigSpans
                treeContext={this.props.treeContext}
                roadId={currentRoad ? currentRoad.id : ''}
                project={project}
                currentBridge={currentBridge}
                bridgeId={currentBridge.id}
                bridges={bridges}
                refRoadlines={refRoadlines}
                currentRoad={currentRoad}
                formular={this.props.formular}
                mode='add'
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'refRoads') {
            return (<SelectRefRoads
                project={project}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'calculate') {
            return (<Calculate
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'cusPropsEdit') {
            return (<CusPropsEdit
                extra={extra}
                refresh={refresh}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'cusTable') {
            return (<CusTable
                extra={extra}
                refresh={refresh}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'relateProp') {
            return (<RelateProp
                extra={extra}
                refresh={refresh}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'propsEdit') {
            return (<PropsEdit
                extra={extra}
                refresh={refresh}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'crossSection') {
            return (<CrossSection
                extra={extra}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'beamLenModal') {
            return (<BeamLenModal
                extra={extra}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'pinnerModal') {
            return (<PinnerModal
                extra={extra}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'positionModal') {
            return (<PositionModal
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'iconMultiSelect') {
            return (<IconMultiSelect
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'adminicula') {
            return (<Adminicula
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'adminiculaDetail') {
            return (<AdminiculaDetail
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'crossSectionDetail') {
            return (<CrossSectionDetail
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        } else if (code === 'crossSectionProp') {
            return (<CrossSectionProp
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        }else if (code === 'AdminicularForm') {
            return (<AdminicularForm
                extra={extra}
                modalContext={this}
                onLoaded={(context) => {
                    this.setState({ context });
                }}
            />);
        }
        
        
        else {
            console.warn('not found ' + code + ' of components');
        }
    }

    updateModal = (option = {}) => {
        this.setState({ ...this.state, ...option });
    }

    render() {
        const {
            onFinish,
            onError,
            parent,
            onCancel,
            okText = '确认',
            cancelText = '取消',
        } = this.props;
        let { visible, event, title, confirmLoading, footer,context } = this.state
        const titles = <ModalDrag title={title}/>
        return (
            visible ? <Modal
                width={"1000px"}
                title={titles}
                centered
                confirmLoading={confirmLoading}
                onCancel={(ccancel) => {
                    if (context && context.onCancel) {
                        context.onCancel()
                    }
                    this.updateModal({ ...this.state, visible: false });
                    this.setState({
                        footer: "default"
                    })
                    if (onCancel) {
                        onCancel();
                    }
                }}
                bodyStyle={{ padding: "12px" }}
                visible={visible}
                okText={okText}
                footer={footer ? [<Button onClick={(ccancel) => {
                    
                    if (context && context.onCancel) {
                       context.onCancel()
                    }
                    this.updateModal({ ...this.state, visible: false });
                    if (onCancel) {
                        onCancel();
                    }
                }}>
                    取消
            </Button>, <Button type="primary" onClick={() => {
                    this.setState({
                        confirmLoading: true
                    })
                    if (context &&context.onConfirm) {
                       context.onConfirm({
                            onSuccess: (values) => {
                                if (parent && parent.modalConfirm) {
                                   parent.modalConfirm(context.state.confirmData, context.state.confirmType)
                                    
                                }
                                this.setState({
                                    confirmLoading: false,
                                    visible: false
                                })
                                if (onFinish) {
                                    onFinish({ values, event });
                                }
                            },
                            onError: (error) => {
                                this.setState({
                                    confirmLoading: false
                                })
                            }
                        });

                    } else {
                        console.warn('modal component not implement method of onConfirm');
                    }
                    this.setState({
                        footer: "default"
                    })
                }} key="test">
                    确定
            </Button>]
                    : null}
                
                cancelText={cancelText}
            >
                {this.parseModelContent(event ? event.code : null)}
            </Modal> : null
        );
    }
}


export default connect(
    ({


    }) => {
        return {

        }
    }
)(ToolConfigModal);
